using Microsoft.AspNetCore.App;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;

namespace Chrome
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                var url = textBox_url.Text;
                var method = comboBox_method.Text;
                var uri = new Uri(url);
                var address = IPAddress.Parse(uri.Host);
                socket.Connect(new IPEndPoint(address, uri.Port));
                var request = new HttpRequest(url, method);
                var bytes = JsonSerializer.SerializeToUtf8Bytes(request);
                socket.Send(bytes);
                var buffer = new byte[1024 * 1024];
                await Task.Factory.StartNew(() =>
                {
                    try
                    {
                        var count = socket.Receive(buffer);
                        if (count > 0)
                        {
                            textBox_response.Text = Encoding.UTF8.GetString(buffer, 0, count);
                        }
                    }
                    catch (Exception)
                    {

                    }
                    finally
                    {
                        socket.Close();
                    }
                });
            }
            catch (Exception)
            {
            }
           
        }
    }
}